package edu.hsbremen.teamscore.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;
import edu.hsbremen.teamscore.R;
import edu.hsbremen.teamscore.listeners.OnModeSelectClickListener;
import edu.hsbremen.teamscore.model.Game;
import edu.hsbremen.teamscore.model.Score;
import edu.hsbremen.teamscore.model.ScoreType;

public class ModeSelect extends Activity {

	protected static final int CHOOSE_GAME = 0;
	protected static final int CHOOSE_MULTI_MODE = 1;
	protected static final int ABOUT = 2;



	private Score _score = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mode_select);

		Button singleMatchBtn = (Button) findViewById(R.id.btnSingleGame);
		singleMatchBtn.setOnClickListener(new OnModeSelectClickListener(
				CHOOSE_GAME, this, GameSelect.class));

		Button multiMatchBtn = (Button) findViewById(R.id.btnMultiplayer);
		multiMatchBtn.setOnClickListener(new OnModeSelectClickListener(
				CHOOSE_MULTI_MODE, this, MultiModeSelect.class));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

		Bundle extras = data.getExtras();

		if (resultCode == RESULT_OK) {

			switch (requestCode) {
			case CHOOSE_GAME:
				String gameName = extras.getString("game");
				this.setupScoreGame(new Game(gameName), ScoreType.SINGLE_GAME);

				Toast.makeText(getApplicationContext(),
						this._score.getGame().getName(), Toast.LENGTH_SHORT)
						.show();
				break;

			case CHOOSE_MULTI_MODE:
				String multiMode = extras.getString("multi_mode");

				Toast.makeText(getApplicationContext(), multiMode,
						Toast.LENGTH_SHORT).show();
				break;

			}
		}
	}

	private void setupScoreGame(Game game, ScoreType sType) {
		if (this._score == null) {
			_score = new Score(game);
			_score.setScoreType(sType);
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id){
		Dialog dialog = null;
		switch(id){
			case ABOUT:
				AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
    			builder2.setMessage(R.string.about)
    			       .setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
    			           public void onClick(DialogInterface dialog, int id) {
    			           }
    			       });
    			dialog = builder2.create();
				break;				
			}
		return dialog;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu_main, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.main_exit:
	    	this.finish();
	    	return true;
	    case R.id.main_about:
	    	showDialog(ABOUT);
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}